<?php

/**
 * Controller_classStudent
 * 班级学生列表
 */

class Controller_classStudent extends MyApp {

	public function __construct()
	{

		parent::__construct();

		$this->model->class    = YOU::loadModel('class');
		$this->model->grade    = YOU::loadModel('grade');
		$this->model->excel    = YOU::loadModel('excel');
		$this->model->student  = YOU::loadModel('student');
		$this->model->semester = YOU::loadModel('semester');
		$this->model->staff    = YOU::loadModel('staff');


		// 年级
		$grade_lists = $this->model->grade->getall();
		foreach ($grade_lists as $key => $value){
			// 班级
			$row = $this->model->class->getListByGrade($value['grade_id']);
			foreach ($row as $k => $v){
				$grade_lists[$key]['class_name'][$v['class_id']] = $v['class_name'];
			}

		}

		$this->tpl->assign("grade_lists", $grade_lists);
	}
	/** 首页 **/
	public function actionIndex()
	{

		$this->tpl->display();
	}
	/** 班级学生列表 **/
	public function actionClassStudentList()
	{

		$class_id		= (int)$this->get->class_id;
		// 年级名称
		$class_data 	= $this->model->class->getinfo($class_id);

		// 搜索
		$student_name	=      $this->get->student_name;
		$student_no		= (int)$this->get->student_no;
		$divide_num		= (int)$this->get->divide_num;
		$student_num	=      $this->get->student_num;
		$student_school	=      $this->get->student_school;

		$where  = "";
		$wheres = "";
		// 点头排序
		if (!empty($student_num)){

			$wheres = " student_no {$student_num}";

		}
		if (!empty($student_school)){

			$wheres = " student_school {$student_school}";

		}
		if (empty($wheres)) {
			$wheres = 'student_id DESC';
		}
		if (!empty($student_name)){

			$where = " AND b.student_name LIKE '%{$student_name}%' ";
			$this->tpl->assign("student_name", $student_name);
		}
		if (!empty($student_no)){
			$where = " AND b.student_no ={$student_no} ";
			$this->tpl->assign("student_no", $student_no);
		}
		if (!empty($divide_num)){
			$where = " AND b.student_seat ={$divide_num} ";
			$this->tpl->assign("divide_num", $divide_num);
		}
		// 分页
		YOU::loadLib('page', "", '0');
		$tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_student` as b WHERE b.class_id='{$class_id}' {$where}");
		$count = $tmp['count'];
		$page_total = ceil($count/PAGE_LIMIT);

		// 当前页
		$p = intval($_GET['p']);
		if ($p == 0){
			$p  = 1;
		}
		if ($p > 1){
			$start = PAGE_LIMIT * ($p-1);
		}else{
			$start = 0;
		}

		$classstudent_lists = $this->db->fetchAll("SELECT * FROM `tbl_student` as b WHERE b.class_id='{$class_id}' {$where} ORDER BY {$wheres} LIMIT {$start}, ".PAGE_LIMIT."");

		$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
		$page->next_page   = '下一页>';
		$page->pre_page    = '<上一页';
		$page->style_path  = "static/page_style";
		$this->tpl->assign("page", $page->show(4, "yahoo"));

		$this->tpl->assign("classstudent_lists", $classstudent_lists);
		$this->tpl->assign("class_id", $class_id);
		$this->tpl->assign("class_data", $class_data);
		$this->tpl->display('classstudent/classstudent.list.tpl.php');
	}
	/** 未分班学生列表 **/
	public function actionStudentList()
	{

		// 搜索
		$student_name	=      $this->get->student_name;
		$student_no		= (int)$this->get->student_no;
		$divide_num		= (int)$this->get->divide_num;

		$where = "";

		if (!empty($student_name)){
			$where = " AND student_name LIKE '%{$student_name}%' ";
			$this->tpl->assign("student_name", $student_name);
		}
		if (!empty($student_no)){
			$where = " AND student_no ={$student_no} ";
			$this->tpl->assign("student_no", $student_no);
		}
		if (!empty($divide_num)){
			$where = " AND divide_num ={$divide_num} ";
			$this->tpl->assign("divide_num", $divide_num);
		}
		// 分页
		YOU::loadLib('page', "", '0');
		$tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_student` WHERE class_id ='' {$where}");
		$count = $tmp['count'];
		$page_total = ceil($count/PAGE_LIMIT);

		// 当前页
		$p = intval($_GET['p']);
		if ($p == 0){
			$p  = 1;
		}
		if ($p > 1){
			$start = PAGE_LIMIT * ($p-1);
		}else{
			$start = 0;
		}

		$classstudent_lists = $this->db->fetchAll("SELECT * FROM `tbl_student`  WHERE class_id ='' {$where} ORDER BY student_id DESC LIMIT {$start}, ".PAGE_LIMIT."");
		
		$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
		$page->next_page   = '下一页>';
		$page->pre_page    = '<上一页';
		$page->style_path  = "static/page_style";
		$this->tpl->assign("page", $page->show(4, "yahoo"));

		$this->tpl->assign("classstudent_lists", $classstudent_lists);
		$this->tpl->display('classstudent/classstudent.list.tpl.php');
	}

	/** -------------------------- 分班批量到入 ----------------------------- **/
	/** 批量导入列表 **/
	public function actionDivideList ()
	{
		// 分页
		YOU::loadLib('page', "", '0');
		$tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_student` WHERE(class_id is not null) AND class_id !=0");
		$count = $tmp['count'];
		$page_total = ceil($count/PAGE_LIMIT);
		// 当前页
		$p = intval($_GET['p']);
		if ($p == 0){
			$p  = 1;
		}
		if ($p > 1){
			$start = PAGE_LIMIT * ($p-1);
		}else{
			$start = 0;
		}

		$classstudent_lists = $this->db->fetchAll("SELECT * FROM `tbl_student`  WHERE class_id is not null AND class_id !=0  ORDER BY class_id ASC  LIMIT {$start}, ".PAGE_LIMIT."");
		foreach ($classstudent_lists as $key => $value){
			// 班级
			$row = $this->model->class->getinfo($value['class_id']);
			$classstudent_lists[$key]['class_name'] = $row['class_name'];
		}

		$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
		$page->next_page   = '下一页>';
		$page->pre_page    = '<上一页';
		$page->style_path  = "static/page_style";
		$this->tpl->assign("page", $page->show(4, "yahoo"));

		$this->tpl->assign("classstudent_lists", $classstudent_lists);
		$this->tpl->display('classstudent/divide.list.tpl.php');

	}
	/** 批量导入列表 **/
	public function actionDivideAdd ()
	{
		// 班级
		$class_lists = $this->model->class->getAll();
		if ($_POST){
			$class_id 	= $this->post->class_id;
			// 上传excel
			$data_lists = $this->model->excel->in("divide");
			//var_dump($data_lists);exit;
			if(empty($data_lists)){
				Helper_Client::alert("请上传文件");
			}else{
				// 删除原纪录
				$this->db->query("DELETE FROM `tbl_student_divide` WHERE class_id='{$class_id}'");
			}

			foreach ($data_lists as $key => $value){

				// 清除空数据
				if (empty($value)) {
					echo 1;
					unset($key);
				}
				foreach ($value as $ke =>$v){

					// 判断学生是否存在
					$student_data = $this->model->student->getInfoBySC($value[3],$value[2]);
					if(empty($student_data)){
						//Helper_Client::alert("学生姓名或学号错误");
					}

				}


				// 写入数据库
				$info = array(

				"class_id"   		=> $class_id,
				"student_seat"   	=> $value[4],
				);

				$this->db->do_update("tbl_student", $info, "student_id='{$student_data['student_id']}'");

			}
			// 跳转
			Helper_Client::location("添加成功", "index.php?a=classstudent.dividelist");
		}

		$this->tpl->assign("class_lists", $class_lists);
		$this->tpl->display('classstudent/divide.add.tpl.php');

	}
	/** 导出模板 **/
	public function actionOutTp()
	{
		// 模板标题
		$this->model->excel->excel_ini['divide']['title'] = "学生分班表";

		// 导出模板 - 6行空数据
		$this->model->excel->outTp("divide");
		exit;
	}
	/** --------------------------------------- 调班 -------------------------------- **/
	public function actionChangeIndex()
	{
		// 名称
		$name = '学生调班';
		// 年级
		$grade_lists  	= $this->model->grade->getall();
		if ($_POST) {
			$grade_id 		= $this->post->grade_id;
			if (!empty($grade_id)) {
				// 跳转
				Helper_Client::location("开始调班", "index.php?a=classstudent.change&grade_id={$grade_id}");
			}
		}
		$this->tpl->assign("grade_lists", $grade_lists);
		$this->tpl->assign("name", $name);
		$this->tpl->display('classstudent/classstudent.index.tpl.php');
	}
	/** 调班 **/
	public function actionChange()
	{
		$grade_id 		= $this->get->grade_id;
		// 年级
		$class_lists  	= $this->model->class->getListByGrade($grade_id);
		// 提交者
		$user_name = $this->model->user->getNameById($this->user['user_id']);
		// 获得当前学期id
		$semester_data = $this->model->semester->getNew();
		$semester_id   = $semester_data['semester_id'];

		if ($_POST){

			$class_id1 		= $this->post->class_id1;
			$class_id2 		= $this->post->class_id2;
			$student_id1 	= $this->post->student_id1;
			$student_id2 	= $this->post->student_id2;
			$student_cause 	= $this->post->student_cause;

			$student_id1 = explode(',',$student_id1);
			$student_id2 = explode(',',$student_id2);

			// 左侧数据
			foreach ($student_id1 as $key => $value){
				// 查看以前的班级
				$student_data  	= $this->model->student->getinfo($value);

				// 写入数据库 当前
				$info = array(

				"class_id"  			=> $class_id1,
				"student_cause"  		=> $student_cause,
				"cause_type"  			=> $this->user['user_id'],
				"update_time"           => date("Y-m-d H:i:s"),

				);
				$this->db->do_update("tbl_student", $info, "student_id='{$value}'");
				if ($student_data['class_id'] != $class_id1) {
					// 历史调班记录写入数据库
					$info = array(

					"grade_id"  			=> $grade_id,
					"class_to"  			=> $class_id1,
					"class_form"  			=> $student_data['class_id'],
					"student_id"  			=> $value,
					"student_cause"  		=> $student_cause,
					"semester_id"  			=> $semester_id,
					"user_id"  				=> $this->user['user_id'],
					"status"                => 1,
					"create_time"           => date("Y-m-d H:i:s"),
					"update_time"           => "0000-00-00 00:00:00",

					);
					$this->db->do_insert("tbl_student_divide", $info);
				}

			}

			if (empty($class_id2)) {
				Helper_Client::alert("请选择调班班级");
			}
			// 右侧数据
			if ($class_id2==9999) {
				foreach ($student_id2 as $key => $value){
					$info = array(
					"class_id"  	=> 0,
					"grade_id"  	=> 0,
					);
					$this->db->do_update("tbl_student", $info, "student_id='{$value}'");
				}
			}else{
				foreach ($student_id2 as $key => $value){
					// 查看以前的班级
					$student_data  	= $this->model->student->getinfo($value);

					// 写入数据库 当前
					$info = array(

					"class_id"  			=> $class_id2,
					"student_cause"  		=> $student_cause,
					"cause_type"  			=> $this->user['user_id'],
					"status"                => 1,
					"create_time"           => date("Y-m-d H:i:s"),
					"update_time"           => "0000-00-00 00:00:00",

					);
					$this->db->do_update("tbl_student", $info, "student_id='{$value}'");
					if ($student_data['class_id'] != $class_id2) {
						// 历史调班记录写入数据库
						$info = array(

						"grade_id"  			=> $grade_id,
						"class_to"  			=> $class_id2,
						"class_form"  			=> $student_data['class_id'],
						"student_id"  			=> $value,
						"student_cause"  		=> $student_cause,
						"semester_id"  			=> $semester_id,
						"user_id"  				=> $this->user['user_id'],
						"status"                => 1,
						"create_time"           => date("Y-m-d H:i:s"),
						"update_time"           => "0000-00-00 00:00:00",

						);
						$this->db->do_insert("tbl_student_divide", $info);
					}

				}
			}
			// 跳转
			Helper_Client::location("调班成功", "index.php?a=classstudent.changeindex");
		}

		$this->tpl->assign("user_name", $user_name);
		$this->tpl->assign("class_lists", $class_lists);
		$this->tpl->display('classstudent/classstudent.change.tpl.php');
	}

	/** 调班历史记录 **/
	public function actionChangeHistory()
	{
		// 分页
		YOU::loadLib('page', "", '0');
		$tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_student_divide` ");
		$count = $tmp['count'];
		$page_total = ceil($count/PAGE_LIMIT);
		// 当前页
		$p = intval($_GET['p']);
		if ($p == 0){
			$p  = 1;
		}
		if ($p > 1){
			$start = PAGE_LIMIT * ($p-1);
		}else{
			$start = 0;
		}

		// 历史记录信息
		$divide_lists  	= $this->db->fetchAll("SELECT * FROM `tbl_student_divide`  ORDER BY divide_id DESC LIMIT {$start}, ".PAGE_LIMIT."");
		foreach ($divide_lists as $key =>$value){
			// 原班级名称
			$row  	= $this->model->class->getinfo($value['class_form']);
			$divide_lists[$key]['class_form'] = $row['class_name'];
			// 目的班级名称
			$row  	= $this->model->class->getinfo($value['class_to']);
			$divide_lists[$key]['class_to'] = $row['class_name'];
			// 学生姓名
			$row  	= $this->model->student->getinfo($value['student_id']);
			$divide_lists[$key]['student_name'] = $row['student_name'];
			$divide_lists[$key]['student_school'] = $row['student_school'];
			// 提交者
			$divide_lists[$key]['user_name'] = $this->model->user->getNameById($value['user_id']);

		}

		$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
		$page->next_page   = '下一页>';
		$page->pre_page    = '<上一页';
		$page->style_path  = "static/page_style";
		$this->tpl->assign("page", $page->show(4, "yahoo"));

		$this->tpl->assign("divide_lists", $divide_lists);
		$this->tpl->display('classstudent/classstudent.history.tpl.php');
	}
	/** ----------------------------------- 未分班学生 -------------------------------- **/
	public function actionClassNoIndex()
	{
		// 名称
		$name = '学生分班';
		// 年级
		$grade_lists  	= $this->model->grade->getall();
		if ($_POST) {
			$grade_id 		= $this->post->grade_id;
			if (!empty($grade_id)) {
				// 跳转
				Helper_Client::location("开始分班", "index.php?a=classstudent.classno&grade_id={$grade_id}");
			}
		}
		$this->tpl->assign("grade_lists", $grade_lists);
		$this->tpl->assign("name", $name);
		$this->tpl->display('classstudent/classstudent.index.tpl.php');
	}
	/** 调班-未分班学生 **/
	public function actionClassNo()
	{
		$grade_id 		= $this->get->grade_id;

		//操作员
		$user_name = $this->model->user->getNameById($this->user['user_id']);

		// 班级
		$class_lists  	= $this->model->class->getListByGrade($grade_id);
		if ($_POST) {

			$class_id2 		= $this->post->class_id2;
			$student_id2 	= $this->post->student_id2;
			$student_cause 	= $this->post->student_cause;
			$student_id2 = explode(',',$student_id2);
			// 右侧数据

			foreach ($student_id2 as $key => $value){
				// 查看以前的班级
				$student_data  	= $this->model->student->getinfo($value);

				// 写入数据库 当前
				$info = array(

				"grade_id"  			=> $grade_id,
				"class_id"  			=> $class_id2,
				"cause_type"  			=> $this->user['user_id'],
				"status"                => 1,
				"create_time"           => date("Y-m-d H:i:s"),
				"update_time"           => "0000-00-00 00:00:00",

				);
				$this->db->do_update("tbl_student", $info, "student_id='{$value}'");

			}
			// 跳转
			Helper_Client::location("调班成功", "index.php?a=classstudent.classnoindex");
		}

		$this->tpl->assign("class_lists", $class_lists);
		$this->tpl->assign("user_name", $user_name);
		$this->tpl->assign("grade_id", $grade_id);
		$this->tpl->display('classstudent/classstudent.not.tpl.php');
	}

	public function actiondivscore(){
		// 当前页
		$p = intval($_GET['p']);
		if ($p == 0){
			$p  = 1;
		}
		if ($p > 1){
			$start = PAGE_LIMIT * ($p-1);
		}else{
			$start = 0;
		}


		$semester_id = $this->get->semester_id;

		if(empty($semester_id)){
			$semester_data = $this->model->semester->getNew();
			$semester_id   = $semester_data['semester_id'];
		}
		$this->tpl->assign('semester_id',$semester_id);
		if($this->get->grade_id == 'new'){//新生处理 按照入学成绩   并添加排名rank
			$first_lists = $this->db->fetchAll("select wq.*,@rownum := @rownum + 1 AS rank  FROM
    (
        SELECT
            *
        FROM
            `tbl_first_score`
            WHERE `semester_id` = {$semester_id}
        ORDER BY
            `first_all` DESC
    ) as wq,(SELECT @rownum := 0) r   LIMIT {$start}, ".PAGE_LIMIT."");
            foreach ($first_lists as $key => $value){
                $first_lists[$key]['rank'] +=$start;
            }
			$this->tpl->assign('first_lists',$first_lists);
			$tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_first_score`  WHERE `semester_id` = {$semester_id} ");

		}elseif(!empty($this->get->grade_id)){//其他学生按照期末成绩分班
            $exam_lists = $this->db->fetchAll("select `exam_id` from `tbl_exam` WHERE `grade_id` = '{$this->get->grade_id}' AND
 `semester_id` = '{$semester_id}' AND `exam_type` = 4 AND `exam_ename` = '期末考' ");
//var_dump($exam_lists);
            $exams =array();
            foreach ($exam_lists as $key => $value){
                $exams[] = $value['exam_id'];
            }
            $exams = implode(',',$exams);//期末考试列表
            if(empty($exams)){
                $exams = 0;
            }
            $student_lists = $this->db->fetchAll("select * from `tbl_student` WHERE `grade_id` = {$this->get->grade_id}");
            foreach ($student_lists as $key => $value){//求总分
                $all = $this->db->fetchRow("select sum(score_value) as sall from `tbl_score` WHERE `student_id`={$value['student_id']} AND `exam_id` in({$exams})");
            $student_lists[$key]['all'] = $all['sall'];
                $cname = $this->db->fetchRow("select class_name from `tbl_class_up` WHERE is_now = 1 AND class_id = {$value['class_id']}");
                $student_lists[$key]['class_name'] = $cname['class_name'];
            }
            //排名
                $sort = array(
                    'direction' => 'SORT_DESC', //排序顺序标志 SORT_DESC 降序；SORT_ASC 升序
                    'field'     => 'all',       //排序字段
                );
                $arrSort = array();
                foreach($student_lists AS $uniqid => $row){
                    foreach($row AS $key=>$value){
                        $arrSort[$key][$uniqid] = $value;
                    }
                }
                if($sort['direction']){
                    @array_multisort($arrSort[$sort['field']], constant($sort['direction']), $student_lists);
                }
                $this->tpl->assign("student_lists",$student_lists);

		}
		$this->tpl->assign('grade_id',$this->get->grade_id);
		YOU::loadLib('page', "", '0');
		$count = $tmp['count'];
		$page_total = ceil($count/PAGE_LIMIT);




		$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
		$page->next_page   = '下一页>';
		$page->pre_page    = '<上一页';
		$page->style_path  = "static/page_style";
		$this->tpl->assign("page", $page->show(4, "yahoo"));

		$grade_lists = $this->db->fetchAll('select * from `tbl_grade` ');
		$this->tpl->assign("grade_lists",$grade_lists);
		$semester_lists = $this->db->fetchAll("select * from `tbl_semester`");
		$this->tpl->assign("semester_lists",$semester_lists);

		$this->tpl->display('classstudent/divscore.tpl.php');


		//$this->tpl->display('classstudent/divscore.tpl.php');
	}


	public function actionSdiv(){
		    //var_dump($_GET);exit;
        $grade_id = $this->get->grade_id;
		$aim = $this->get->aim_id;
        if(empty($aim)){
            $aim = $grade_id;
        }
		$this->tpl->assign('aim',$aim);
		$check = $this->get->check;
		if(($grade_id=='new')&&empty($aim)){
			Helper_Client::alert("请选择目标年级");
		}
		if($aim=='new'){
            $row = $this->db->fetchAll("select * from tbl_grade_up WHERE grade_code=1 and is_now =1");
            $aim = $row['grade_id'];
		}
		$class_lists = $this->db->fetchAll("select * from tbl_class_up where `grade_id` ={$aim} AND is_now =1 ");

		$len = count($class_lists);

        //分页
//		$p = intval($_GET['p']);
//		if ($p == 0){
//			$p  = 1;
//		}
//		if ($p > 1){
//			$start = PAGE_LIMIT * ($p-1);
//		}else{
//			$start = 0;
//		}


		$semester_id = $this->get->semester_id;

		if(empty($semester_id)){
			$semester_data = $this->model->semester->getNew();
			$semester_id   = $semester_data['semester_id'];
		}
		$this->tpl->assign('semester_id',$semester_id);
		if($this->get->grade_id == 'new'){
			$first_lists = $this->db->fetchAll("select wq.*,@rownum := @rownum + 1 AS rank  FROM
    (
        SELECT
            *
        FROM
            `tbl_first_score`
            WHERE `semester_id` = {$semester_id}
        ORDER BY
            `first_all` DESC
    ) as wq,(SELECT @rownum := 0) r  ");// LIMIT {$start}, ".PAGE_LIMIT."

			//$tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_first_score`  WHERE `semester_id` = {$semester_id} ");

			$i = 0; $status = 1;
			foreach ($first_lists as $key => $value){
				if($i>=$len){//if len
					$i = $len - 1 ;
					$status = 0;
				}
				if($i<0){
					$i = 0;
					$status = 1;
				}

				$first_lists[$key]['class_id'] = $class_lists[$i]['class_id'];
				$first_lists[$key]['class_name'] = $class_lists[$i]['class_name'];
				$status?$i++:$i--;
				if($check){
					$info = array(

					"student_name"      	=> $first_lists[$key]['student_name'],
					"student_school"      => $first_lists[$key]['student_school'],
					"class_id"       => $first_lists[$key]['class_id'],
					"grade_id"      => $aim,
					"create_time"           => date("Y-m-d H:i:s"),
					"update_time"           => "0000-00-00 00:00:00",

					);
					$this->db->do_insert("tbl_student", $info);
				}


			}//按照班级展示
            if($check){Helper_Client::alert("成功分班！");}
			$sort = array(
			'direction' => 'SORT_ASC', //排序顺序标志 SORT_DESC 降序；SORT_ASC 升序
			'field'     => 'class_id',       //排序字段
			);
			$arrSort = array();
			foreach($first_lists AS $uniqid => $row){
				foreach($row AS $key=>$value){
					$arrSort[$key][$uniqid] = $value;
				}
			} //var_dump($arr);exit;
			if($sort['direction']){
				@array_multisort($arrSort[$sort['field']], constant($sort['direction']), $first_lists);
			}
			$this->tpl->assign('first_lists',$first_lists);
		}elseif(!empty($this->get->grade_id)){//其他学生按照期末成绩分班
            $exam_lists = $this->db->fetchAll("select `exam_id` from `tbl_exam` WHERE `grade_id` = '{$this->get->grade_id}' AND
 `semester_id` = '{$semester_id}' AND `exam_type` = 4 AND `exam_ename` = '期末考' ");

            $exams =array();
            foreach ($exam_lists as $key => $value){
                $exams[] = $value['exam_id'];
            }
            $exams = implode(',',$exams);//期末考试列表
            if(empty($exams)){
                $exams = 0;
            }
            //var_dump($exams);
            $student_lists = $this->db->fetchAll("select * from `tbl_student` WHERE `grade_id` = {$this->get->grade_id}");
            foreach ($student_lists as $key => $value){//求总分
                $all = $this->db->fetchRow("select sum(score_value) as sall from `tbl_score` WHERE `student_id`={$value['student_id']} AND `exam_id` in({$exams})");
                $student_lists[$key]['all'] = $all['sall'];
                $cname = $this->db->fetchRow("select class_name from `tbl_class_up` WHERE is_now = 1 AND class_id = {$value['class_id']}");
                $student_lists[$key]['class_name'] = $cname['class_name'];

            }
            //排名
            $sort = array(
                'direction' => 'SORT_DESC', //排序顺序标志 SORT_DESC 降序；SORT_ASC 升序
                'field'     => 'all',       //排序字段
            );
            $arrSort = array();
            foreach($student_lists AS $uniqid => $row){
                foreach($row AS $key=>$value){
                    $arrSort[$key][$uniqid] = $value;
                }
            }
            if($sort['direction']){
                @array_multisort($arrSort[$sort['field']], constant($sort['direction']), $student_lists);
            }//开始分班
            $i = -1;
            foreach ($student_lists as $key => $value){
                if($i>=$len){//if len
                    $i = $len - 1 ;
                    $status = 0;
                }
                if($i<0){
                    $i = 0;
                    $status = 1;
                }
                $oldname = $this->db->fetchRow("select * from `tbl_class_up` WHERE is_now = 1 AND class_id = '{$student_lists[$key]['class_id']}'");
                $student_lists[$key]['old_cname'] = $oldname['class_name'];
                $student_lists[$key]['class_id'] = $class_lists[$i]['class_id'];
                $student_lists[$key]['class_name'] = $class_lists[$i]['class_name'];
                $student_lists[$key]['rank'] = $key+1;
                //var_dump($class_lists);exit;
                $status?$i++:$i--;
                if($check){
                    $info = array(
                        "class_id"       => $student_lists[$key]['class_id'],
                        "update_time"           => date("Y-m-d H:i:s"),

                    );
                    $this->db->do_update("tbl_student", $info, "student_id='{$student_lists[$key]['student_id']}'" );
                }


            }//按照班级展示
            if($check){Helper_Client::alert("成功分班！");}
            //按照新班级展示
            $sort = array(
                'direction' => 'SORT_ASC', //排序顺序标志 SORT_DESC 降序；SORT_ASC 升序
                'field'     => 'class_id',       //排序字段
            );
            $arrSort = array();
            foreach($student_lists AS $uniqid => $row){
                foreach($row AS $key=>$value){
                    $arrSort[$key][$uniqid] = $value;
                }
            } //var_dump($arr);exit;
            if($sort['direction']){
                @array_multisort($arrSort[$sort['field']], constant($sort['direction']), $student_lists);
            }

            $this->tpl->assign("student_lists",$student_lists);

        }
		$this->tpl->assign('grade_id',$this->get->grade_id);
//		YOU::loadLib('page', "", '0');
//		$count = $tmp['count'];
//		$page_total = ceil($count/PAGE_LIMIT);



//
//		$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
//		$page->next_page   = '下一页>';
//		$page->pre_page    = '<上一页';
//		$page->style_path  = "static/page_style";
//		$this->tpl->assign("page", $page->show(4, "yahoo"));


		$this->tpl->display('classstudent/divs.tpl .php');
	}
	public  function  actionTemporary(){
        $data = $this->db->fetchRow("select data from `tbl_temporary` WHERE temporary_id=1 ");
        $datas = unserialize($data['data']);

        $sort = array(
            'direction' => 'SORT_ASC', //排序顺序标志 SORT_DESC 降序；SORT_ASC 升序
            'field'     => 'class',       //排序字段
        );
        $arrSort = array();
        if(empty($datas)){$datas=array();}
        foreach($datas AS $uniqid => $row){
            foreach($row AS $key=>$value){
                $arrSort[$key][$uniqid] = $value;
            }
        } //var_dump($arr);exit;
        if($sort['direction']){
            @array_multisort($arrSort[$sort['field']], constant($sort['direction']), $datas);
        }

        $this->tpl->assign("datas", $datas);

        $this->tpl->display('classstudent/temporary.tpl.php');
    }
    public function actionTemin(){
        if($_POST){
            $data = $this->model->excel->in('temporary');
            $num = (int)$this->post->num;
            if(empty($num)){$num=2;}
            $i = 1;
            foreach ($data as $key=>$value){
                $data[$key] = array_slice($data[$key],0,2);
                $data[$key]['class'] = $i.'班';
                $i++;
                if($i>$num){$i=1;}
            }

            $datas = serialize($data);
            //var_dump($datas);exit;
            $info = array(
                "data"=>$datas,
                "update_time"=>date("Y-m-d",time()),
                "status"=>1,
            );
            $this->db->do_update("tbl_temporary", $info, " temporary_id=1 " );
            Helper_Client::location("导入成功", "index.php?a=classstudent.temporary");
        }
        $this->tpl->display('classstudent/temporaryin.tpl.php');

    }
    public function actionTemout(){
        $data_lists = array(array());
        $this->model->excel->outData("temporary", $data_lists);
    }
}

?>